<?php

class ScoreManager_ScoreTopController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {

        $this->view->yearSemester = $this->getRequest()->getPost('yearSemester');
        $this->view->selectGrade = $this->getRequest()->getPost('select-grade');

        // 考試設定
        if ( $this->view->yearSemester and $this->view->selectGrade) {
            $tempArr = explode("-",$this->view->yearSemester);
            $year = (int) $tempArr[0];
            $semester = (int)$tempArr[1];
            $grade = (int)$this->view->selectGrade;
            $examine = new Semester_Model_SemesterExamine();
            $examineSet = $examine->getExaminSet($year, $semester, $grade);
            // 考試設定
            $this->view->examineSet = $examineSet;
        }

        $stage = (int)$this->_getParam('stage');
        if ($stage) {
            // 階段別
            $this->view->stage = $stage;
            // 取出班級階段成績資料
            $statistics = new Score_Model_Statistics($grade, $this->view->yearSemester,$stage);
            // 加計平時成績
            $this->view->plusCount = (int)$this->_getParam('plusCount');
            // 加權計算
            $this->view->rateCount = (int)$this->_getParam('rateCount');
            //  排名方式
            $this->view->orderKind = (int) $this->_getParam('orderKind');
            // 排名數
            $numbers = ($this->_getParam('numbers') )?(int) $this->_getParam('numbers') :50;
            $this->view->numbers = $numbers ;
            // 課程名稱
            $this->view->subject = $statistics->getSubject();
            // 記錄選取科目
            $this->view->selSubject = $this->_getParam('selSubject');
            if ($this->_getParam('act') == 'downloadGroupScore')
            $numbers = 0;

            if ($this->_getParam('act')) {
                $res = $statistics->getTopScore($stage,
                $this->view->orderKind,
                $this->view->selSubject,
                $this->view->plusCount,
                $this->view->rateCount,
                $numbers );
                $this->view->topList = $res;
                switch ($this->_getParam('act')) {
                    // 下載  CVS 檔
                    case 'downloadCSV':
                        Zend_Controller_Action_HelperBroker::getStaticHelper('layout')->disableLayout();
                        $fileName =  $this->view->yearSemester.'-'.$grade.'-'.$stage.'.csv';
                        $this->getResponse()->setHeader("Pragma","public");
                        $this->getResponse()->setHeader("Expires", 0);
                        $this->getResponse()->setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
                        $this->getResponse()->setHeader("Cache-Control: ","private",false);
                        $this->getResponse()->setHeader("Content-Type",'text/csv');
                        $this->getResponse()->setHeader("Content-Disposition","attachment; filename=\"".$fileName."\";");
                        set_time_limit(0);
                        $this->render('csv');
                        break;
                    case 'downloadGroupScore':
                        Zend_Controller_Action_HelperBroker::getStaticHelper('layout')->setLayout('print-style');

                        $this->render('group-score');
                        break;
                }
            }
        }
    }


}

